package com.degon.hsyl.customchatapp.ui;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.degon.hsyl.customchatapp.R;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import java.util.Calendar;
import java.util.List;

import io.rong.imlib.RongIMClient;
import io.rong.imlib.model.Conversation;
import io.rong.imlib.model.Message;
import io.rong.push.RongPushClient;

public class ConversationListActivity extends AppCompatActivity {

    private String token;
    private String userId;

    private ImageView backBtn;
    private RecyclerView conversationListView;
    private SmartRefreshLayout refreshLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_conversation_list);

        initViews();

        Intent intent = this.getIntent();
        token = intent.getStringExtra("token");
        userId = intent.getStringExtra("userId");

        //获取消息列表
        getAllConversation();

        backBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                refreshLayout.finishRefresh(1000);
                getAllConversation();
            }
        });

        RongIMClient.setOnReceiveMessageListener(new RongIMClient.OnReceiveMessageWrapperListener() {
            @Override
            public boolean onReceived(Message message, int i, boolean b, boolean b1) {
                getAllConversation();
                return true;
            }
        });
    }

    private void initViews() {
        backBtn = findViewById(R.id.消息页_返回按钮);
        conversationListView = findViewById(R.id.消息列表);
        refreshLayout = findViewById(R.id.消息页_下拉刷新组件);
    }

    private void getAllConversation() {

        RongIMClient.getInstance().getConversationList(new RongIMClient.
                ResultCallback<List<Conversation>>() {
            @Override
            public void onSuccess(List<Conversation> conversations) {
                if (conversations == null) {
                    ToastUtils.showShort("消息队列为空");
                } else {
                    ToastUtils.showShort(conversations.get(0).getLatestMessage().getUserInfo().getName());
                }
            }

            @Override
            public void onError(RongIMClient.ErrorCode errorCode) {

            }
        });

    }
}